import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class turret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class turret  extends Enemy
{
    Zony z;
    int counter=0;
    double a,b,x,y;

    protected void addedToWorld(World world)
    {
        setImage(((it) getWorld()).getTurret()); 
        Zony it = (Zony) getWorld().getObjects(Zony.class).get(0);
        z=it;
    }  
    
    public void act() 
    {
        setRotation((int)(180*Math.atan2(z.getY()-getY(),z.getX()-getX())/Math.PI)-90);
        a=5*Math.cos(Math.PI*(getRotation()+90)/180.0);
        b=5*Math.sin(Math.PI*(getRotation()+90)/180.0);
        counter++;
        if(counter%5==0){
            counter=0;
            x=getX();
            y=getY();
            while((Math.abs(z.getY()-y)>20 || Math.abs(z.getX()-x)>20) && ((it)getWorld()).ok((int)x,(int)y)){
                x=x+a;
                y=y+b;
            }
            if(((it)getWorld()).ok((int)x,(int)y))
                getWorld().addObject(new projectile(2,a,b,false),getX()+(int)(2*a),getY()+(int)(2*b));
            }
    }    
    
    public void hurt(int x){
    }
}
